{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2d434333",
   "metadata": {},
   "source": [
    "---\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61f86041",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "> 1. 代码的注释\n",
    "> 2. 用户输入\n",
    "> 3. 格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34ec12e4",
   "metadata": {},
   "source": [
    "## 代码的注释"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa507d46",
   "metadata": {},
   "source": [
    "## 用户输入\n",
    "> 1.input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "49d5670d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123456\n",
      "admin\n",
      "123456\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统\n",
    "\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "20b393f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123456\n",
      "admin\n",
      "123456\n",
      "欢迎admin登陆\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代1 ：增加判断功能\n",
    "# 假设我们数据库有用户信息：账号：admin，密码：123456\n",
    "username1 = input(\"账号:\")\n",
    "password1 = input(\"密码:\")\n",
    "\n",
    "print(username)\n",
    "print(password)\n",
    "\n",
    "# 情景一：输入信息正确，登陆成功\n",
    "if username1 == username and password1 == password:\n",
    "    print(\"欢迎\"+username1+\"登陆\")\n",
    "# 情景二：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4b73e37d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:admin\n",
      "密码:123456\n",
      "欢迎admin登陆\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代2：增加循环功能\n",
    "# 假设我们数据库有用户信息：账号：admin，密码：123456\n",
    "for i in range(3,0,-1): # i 是int\n",
    "    username1 = input(\"账号:\")\n",
    "    password1 = input(\"密码:\")\n",
    "\n",
    "    # 情景一：输入信息正确，登陆成功\n",
    "    if username1 == username and password1 == password:\n",
    "        print(\"欢迎\"+username1+\"登陆\")\n",
    "        break # 终止循环\n",
    "    # 情景二：账号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请1个小时后再来尝试1\")\n",
    "        print(\"账号不存在或密码不正确\")\n",
    "        print(\"您还有\"+str(i-1)+\"次尝试的机会\") # str强制将int转换成str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0925c796",
   "metadata": {},
   "source": [
    "## 2.格式化输出\n",
    "> 1. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "59d713b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：n\n",
      "年龄：12\n",
      "工作：it\n",
      "收入：20000\n",
      "地区：jd\n",
      "---INFO OFn------\n",
      "年龄：12\n",
      "工作：it\n",
      "收入：20000\n",
      "地区：jd\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统\n",
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''---INFO OF'''+ name +'''------'''+'''\n",
    "年龄：'''+age+'''\n",
    "工作：'''+job+'''\n",
    "收入：'''+salary+'''\n",
    "地区：'''+reg\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0ff19a64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄：24\n",
      "24 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，其都以字符串形式返回\n",
    "age = input(\"年龄：\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3e4ea5aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用户注册系统 方法二：占位符 %s str "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "84a643de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：js\n",
      "年龄：26\n",
      "工作：dj\n",
      "收入：aj\n",
      "地区：fj\n",
      "-----INFO OF js-----\n",
      "年龄：26\n",
      "工作：dj\n",
      "收入：aj\n",
      "地区：fj\n"
     ]
    }
   ],
   "source": [
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\"))# 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "\n",
    "info = '''-----INFO OF %s-----\n",
    "年龄：%d\n",
    "工作：%s\n",
    "收入：%s\n",
    "地区：%s'''%(name,age,job,salary,reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e7a0b738",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：dj\n",
      "年龄：55\n",
      "工作：sj\n",
      "收入：dj\n",
      "地区：aj\n",
      "-----INFO OF dj-----\n",
      "年龄：55\n",
      "工作：sj\n",
      "收入：dj\n",
      "地区：aj\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法三：format() 格式化输出函数，参数传递，不需要考虑位置11\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\"))# 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "\n",
    "info = '''-----INFO OF {姓名}-----\n",
    "年龄：{年龄}\n",
    "工作：{工作}\n",
    "收入：{收入}\n",
    "地区：{地区}'''.format(姓名=name,工作=job,年龄=age,收入=salary,地区=reg)\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01f3277c",
   "metadata": {},
   "source": [
    "# 课后练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5ae4fe9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户\n",
    "# 让用户输入商品编号进行商品的购买 input\n",
    "# 用户选择商品后，检查用户余额是否够，若够则直接扣款，不够则提醒用户 if\n",
    "# 用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额 input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c7e6b9ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的账号密码\n",
      "账号：123456\n",
      "密码：456123\n",
      "登录成功，欢迎回来\n",
      "请输入您的支付宝余额：122\n",
      "支付宝余额为：122\n",
      "-----商品------\n",
      "        黄瓜: 2元  编号:a\n",
      "        西瓜：  3元  编号:b\n",
      "        矿泉水：3元  编号:c\n",
      "        瓜子：  2元  编号:d\n",
      "请输入你要购买的商品的编号：c\n",
      "扣除2元\n"
     ]
    }
   ],
   "source": [
    "# 支付宝登录 假设支付宝账号和密码\n",
    "age_1 = \"123456\"\n",
    "age_2 = \"456123\"\n",
    "# 商品编号\n",
    "print(\"请输入您的账号密码\")\n",
    "# 输入账号密码\n",
    "for i in range(3,0,-1):\n",
    "    age_11 = input(\"账号：\")\n",
    "    age_22 = input(\"密码：\")\n",
    "    if age_11 == age_1 and age_22 == age_2:\n",
    "        print(\"登录成功，欢迎回来\")\n",
    "        age_3 = (input(\"请输入您的支付宝余额：\")) # 支付宝余额\n",
    "        print(\"支付宝余额为：\"+age_3)\n",
    "        # 给商品编号\n",
    "        a = \"黄瓜\"\n",
    "        b = \"西瓜\"\n",
    "        c = \"矿泉水\"\n",
    "        d = \"瓜子\"\n",
    "        # 打印商品列表给用户\n",
    "        商品列表 = '''-----商品------\n",
    "        %s: 2元  编号:a\n",
    "        %s：  3元  编号:b\n",
    "        %s：3元  编号:c\n",
    "        %s：  2元  编号:d''' %(a,b,c,d)\n",
    "        print(商品列表)\n",
    "        购买商品 = input(\"请输入你要购买的商品的编号：\")\n",
    "        if 购买商品 == \"a\":\n",
    "            a_1 = 2 # 给西红柿价格赋值\n",
    "            if int(age_3) - a_1 >= 0:\n",
    "                print(\"扣除\"+str(a_1)+\"元\")\n",
    "                age_3 = int(age_3) - a_1\n",
    "                购买商品 = \"西红柿\"\n",
    "            else:\n",
    "                print(\"您的余额不足，无法购买\")\n",
    "        elif 购买商品 == \"b\":\n",
    "            b_1 = 2\n",
    "            if int(age_3) - b_1 >= 0:\n",
    "                print(\"扣除\"+str(b_1)+\"元\")\n",
    "                age_3 = int(age_3) - b_1\n",
    "                购买商品 = \"西瓜\"\n",
    "            else:\n",
    "                print(\"您的余额不足，无法购买\")\n",
    "        elif 购买商品 == \"c\":\n",
    "            c_1 = 2\n",
    "            if int(age_3) - c_1 >= 0:\n",
    "                print(\"扣除\"+str(c_1)+\"元\")\n",
    "                age_3 = int(age_3) - c_1\n",
    "                购买商品 = \"矿泉水\"\n",
    "            else:\n",
    "                print(\"您的余额不足，无法购买\")\n",
    "        else:\n",
    "            d_1 = 2\n",
    "            if int(age_3) - d_1 >= 0:\n",
    "                print(\"扣除\"+str(d_1)+\"元\")\n",
    "                age_3 = int(age_3) - d_1\n",
    "                购买商品 = \"瓜子\"\n",
    "            else:\n",
    "                print(\"您的余额不足，无法购买\")\n",
    "        \n",
    "        break\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请1小时后重新尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码错误\")\n",
    "            print(\"您还有\"+str(i-1)+\"次机会，请重新输入\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e47ba478",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
